package com.exam.system.controller;

import com.exam.system.pojo.Admin;
import com.exam.system.pojo.Permission;
import com.exam.system.pojo.Role;
import com.exam.system.service.AdminService;
import com.exam.system.service.PermissionService;
import com.exam.system.service.RoleService;
import com.exam.system.util.AjaxResult;
import com.exam.system.util.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Classname ManagerController
 * @Description 后台通用控制器
 * @Date 2019/8/9 22:11
 * @Created by WDD
 */
@Controller
@RequestMapping("/manager")
public class ManagerController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private AdminService adminService;
    @Autowired
    private PermissionService permissionService;

    /**
     * 跳转登录页面
     * @return
     */
    @GetMapping("/login")
    public String login(){
        return "/manager/login";
    }

    /**
     * 异步表单登录
     * @param admin
     * @param session
     * @return
     */
    @PostMapping("/login")
    @ResponseBody
    public AjaxResult dologin(Admin admin, HttpSession session){
        if(StringUtils.isEmpty(admin.getUsername())){
            return AjaxResult.fail("用户名不能为空");
        }
        if(StringUtils.isEmpty(admin.getPassword())){
            return AjaxResult.fail("密码不能为空");
        }
        // 查询用户是否存在 并返回用户数据
        try {
            Admin ad = adminService.login(admin);
            if(StringUtils.isEmpty(ad)){
                return AjaxResult.fail("该用户不存在");
            }else if (ad.getStatus().equals(0)){
                return AjaxResult.fail("该用户尚未通过审核");
            }else{
                session.setAttribute(Const.ADMIN,ad);
                //获取角色列表，存入session
                if(session.getAttribute(Const.ROLE_LIST) == null){
                    List<Role> roleList = roleService.selectAll();
                    session.setAttribute(Const.ROLE_LIST,roleList);
                }
                return AjaxResult.success("登录成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            AjaxResult.fail("系统错误,请刷新");
        }
        //不会执行到这一步,因此设置未null
        return null;
    }

    /**
     * 跳转后台页面
     * @return
     */
    @GetMapping("/index")
    public String index(){
        return "/manager/index";
    }

    /**
     * 异步加载后台主页
     * @return
     */
    @GetMapping("/console")
    public String console(){
        return "/manager/console";
    }

    /**
     * 异步加载权限树
     * @param session
     * @return
     */
    @PostMapping("/treeMenu")
    @ResponseBody
    public Object treeMenu(HttpSession session){
        //判断session中是否已存在,若存在直接返回
        if(!StringUtils.isEmpty(session.getAttribute(Const.PERMISSION_LIST))){
            return session.getAttribute(Const.PERMISSION_LIST);
        }
        //通过adminId 查询 所相对应的 权限 放入session
        Admin admin = (Admin) session.getAttribute(Const.ADMIN);
        List<Permission> permissionList = permissionService.selectByAdminId(admin.getId());
        session.setAttribute(Const.PERMISSION_LIST,permissionList);
        return permissionList;
    }

    /**
     * 登出
     * @param session
     * @return
     */
    @GetMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "/manager/login";
    }

    /**
     * 管理员信息页面
     * @param session
     * @param model
     * @return
     */
    @GetMapping("/info")
    public String info(HttpSession session, Model model){
        Admin admin = (Admin) session.getAttribute(Const.ADMIN);
        Role role = roleService.selectById(admin.getRoleId());
        model.addAttribute(Const.ROLE,role);
        return "manager/common/info";
    }

    /**
     * 修改
     * @param admin
     * @param session
     * @return
     */
    @PostMapping("/editInfo")
    @ResponseBody
    public AjaxResult editInfo(Admin admin,HttpSession session){
        Admin ad = (Admin) session.getAttribute(Const.ADMIN);
        ad.setUsername(admin.getUsername());
        ad.setEmail(admin.getEmail());
        ad.setPhone(admin.getPhone());
        int count = adminService.editByAdmin(ad);
        if(count >= 1){
            return AjaxResult.success("修改成功");
        }
        return AjaxResult.fail("修改失败");
    }

    /**
     * 跳转修改密码页面
     * @return
     */
    @GetMapping("/password")
    public String password(){
        return "manager/common/password";
    }

    /**
     * 修改密码
     * @param session
     * @param password
     * @param newpassword
     * @param repassword
     * @return
     */
    @PostMapping("/editPassword")
    @ResponseBody
    public AjaxResult editPassword(HttpSession session,String password,String newpassword,String repassword){
        Admin admin = (Admin) session.getAttribute(Const.ADMIN);
        if(!password.equals(admin.getPassword())){
            return AjaxResult.fail("原密码错误");
        }
        if(!newpassword.equals(repassword)){
            return AjaxResult.fail("密码不一致");
        }
        admin.setPassword(newpassword);
        int count = adminService.editByAdmin(admin);
        if(count >= 1){
            return AjaxResult.success("修改密码成功");
        }
        return AjaxResult.fail("系统错误");
    }
}
